import Big from "big.js";

// 给表格添加总计
export function tableSummaries(keys) {
  return (param) => {
    const { columns, data } = param;
    const sums = [];
    const summarKey = keys || [];
    columns.forEach((column, index) => {
      if (index === 0 || !summarKey.includes(column.property)) {
        sums[index] = index === 0 ? "总计" : "";
        return;
      }
      const values = data.map((item) => Number(item[column.property]));
      if (!values.every((value) => Number.isNaN(value))) {
        sums[index] = `${values.reduce((prev, curr) => {
          const value = Number(curr);
          if (!Number.isNaN(value)) {
            return new Big(prev).plus(curr);
          } else {
            return prev;
          }
        }, 0)}`;
      } else {
        sums[index] = "N/A";
      }
    });
  
    return sums;
  }
}